<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<!-- 
    题目： 实现get方法
    let obj = {
        id: 12,
        message: 'hello',
        data: [
            {
                name: 'javascript'
            }
        ]
    }
    get(obj, 'message')  // hello
    get(obj, 'data[0].name')  // javascript
 -->
</body>
<script>
    function get(obj, key){
        if (typeof obj !== 'object' || obj === null || typeof key !== 'string') return
        if (!key.includes('[') && !key.includes('.')){ // 不包含[] 和 .
            return obj[key]
        } 
        if (key.includes('[') && !key.includes('.')){ // 只包含[]，不包含.
            let str = key.split('[')
            let res = obj[str[0]]
            for(let i = 1; i < str.length; i++){
                let index = str[i].split(']')[0]
                res = res[index]
            }
            return res
        }
        if (key.includes('.') && !key.includes('[')){ // 只包含. ，不包含[]
            let str = key.split('.')
            let res = obj[str[0]]
            for(let i = 1; i < str.length; i++){
                res = res[str[i]]
            }
            return res
        }
        if (key.includes('.') && key.includes('[')){  // 包含 . 和 []
            let arr = key.split('.')
            if (key.split('.')[0].includes('[')){
                arr= key.split('[')
            }
            let res = obj[arr[0]]
            let str = ''
            console.log('arr',arr, res)            
            let _key = key.slice(arr[0].length-1)
            console.log('删除第一项后arr', _key)
            for(let i= 0; i < _key.length; i++){
                str += _key[i]
                console.log('for中str', str)
                if(_key[i]==='.'){
                    str = str.split('.')[0]
                    console.log('是.', str)
                    res = str && res[str] || res
                    str = ''
                    console.log('res', res)
                }
                if(_key[i]==='[' || _key[i]===']'){
                    str = str.split('[')[0]
                    if(_key[i]===']') str = str.split(']')[0]
                    console.log('是[ ]', str)
                    res = str && res[str] || res
                    str = ''
                    console.log('res', res)
                }
            }
            if(str) res = res[str]
            return res
        }
    }

    function _get(obj, key){
        if (typeof obj !== 'object' || obj === null || typeof key !== 'string') return
        let str = ''
        for(let i= 0; i < key.length; i++){
            str += key[i]
            if(key[i]==='.' || key[i]==='[' || key[i]===']'){
                str = str.split(key[i])[0]
                obj = obj && obj[str] || obj
                str = ''
            }            
        }
        if(str) obj = obj[str]
        return obj
    }

    let obj = {
        id: 12,
        message: 'hello',
        data: [
            {
                obj: {
                    arr: ['javascript']
                }
            }
        ],
        obj: {
            obj:{
                obj:{
                    name: 'obj'
                }
            }
        },
        arr: [[['arr']]]
    }

    console.log( 'res>>>>>>>>', _get(obj, 'obj.obj.obj.name') )
    console.log( 'res>>>>>>>>', _get(obj, 'arr[0][0][0]') )
    console.log( 'res>>>>>>>>', _get(obj, 'message') )
    console.log( 'res>>>>>>>>', _get(obj, 'data[0].obj.arr[0]') )

</script>
</html>